package com.blog.zip.controller.web;

import com.blog.zip.mapper.ArticleMapper;
import com.blog.zip.mapper.CategoryMapper;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Slf4j
@RestController
public class WebCategory {

    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ArticleMapper articleMapper;

    @GetMapping("/category/get")
    public Map<String, Object> getCategory(String type) {
        List<Map<String, Object>> list = new ArrayList<>();
        if (type != null) {
            list = categoryMapper.getCategoryByType(type);
        } else {
            list = categoryMapper.getCategory();
        }
        // 找到该分类下的所有文章
        for (Map m : list) {
            List articles = categoryMapper.getCategoryArticle((String) m.get("oId"));
            // 文章没有去重
            m.put("articles", articles);
        }
        Map<String, Object> map = new HashMap();
        map.put("code", 200);
        map.put("data", list);
        return map;
    }
}
